const express = require('express');

const app = express();

const mw = function(req, res, next) {
  // 中间件共享req res中的属性
  req.a = 111
  next();
}

const mw2 = function(req, res, next) {
  // 中间件共享req res中的属性
  req.b = 222
  next();
}

app.use((req, res, next) => {
  console.log('最简单的中间件函数');
  // 中间件共享req res中的属性
  req.startTime = new Date();
  next();
})

app.use((req, res, next) => {
  console.log('第二个中间件函数');
  req.endTime = new Date();
  next();
})

app.get('/', (req, res) => {
  console.log('调用了/', req.startTime, req.endTime);
  res.send('Hello World!');
});

// 局部生效中间件
app.get('/user', [mw, mw2], (req, res) => {
  console.log('调用了user', req.startTime, req.endTime);
  console.log('局部的中间件函数', req.a, req.b);
  res.send('user Hello World!');
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
})